import type { App } from "vue";
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";
import { setupRouterGuards } from "./guards";

/** 默认布局 */
export const LAYOUT = () => import("@/layouts/default/index.vue");
/** 大屏布局 */
export const LS_LAYOUT = () => import("@/layouts/large-screen/index.vue");
/** 实验室布局 */
export const POC_LAYOUT = () => import("@/layouts/poc/index.vue");

export const routes: RouteRecordRaw[] = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/framework/login/index.vue"),
    meta: { title: "登录" },
  },
  {
    path: "/",
    name: "root",
    redirect: "/home",
    component: LAYOUT,
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("@/views/business/index.vue"),
      },
    ],
  },
  {
    path: "/sys",
    name: "sys",
    component: LAYOUT,
    redirect: "/sys/menu",
    children: [
      {
        path: "/sys/menu",
        name: "menu",
        component: () => import("@/views/sys/menu/index.vue"),
      },
      {
        path: "/sys/user",
        name: "user",
        component: () => import("@/views/sys/user/index.vue"),
      },
      {
        path: "/sys/role",
        name: "role",
        component: () => import("@/views/sys/role/index.vue"),
      },
    ],
  },
  {
    path: "/poc",
    name: "poc",
    component: POC_LAYOUT,
    redirect: "/poc/home",
    children: [
      {
        path: "home",
        name: "pocHome",
        component: () => import("@/views/poc/index.vue"),
        meta: { title: "实验室", icon: "fluent-emoji-flat-bubbles" },
      },
      {
        path: "file-worker",
        name: "fileWorker",
        component: () => import("@/views/poc/file-worker.vue"),
        meta: { title: "文件读取", icon: "document-folder" },
      },
      {
        path: "gis",
        name: "gis",
        component: () => import("@/views/poc/gis.vue"),
        meta: { title: "地图实验", icon: "cesium" },
      },
      {
        path: "plot",
        name: "plot",
        component: () => import("@/views/poc/d3-plot-view.vue"),
        meta: { title: "D3 Plot 曲线", icon: "logos-d3" },
      },
      {
        path: "timeline",
        name: "timeline",
        component: () => import("@/views/poc/d3-timeline-view.vue"),
        meta: { title: "D3 Timeline", icon: "logos-d3" },
      },
      {
        path: "tree-view",
        name: "treeView",
        component: () => import("@/views/poc/g6-tree-view.vue"),
        meta: { title: "G6 Tree", icon: "flat-color-icons-engineering" },
      },
      {
        path: "pixi-view",
        name: "pixiView",
        component: () => import("@/views/poc/pixi-view.vue"),
        meta: { title: "Pixi View", icon: "logos-pixijs" },
      },
      {
        path: "svg-js-view",
        name: "svgJsView",
        component: () => import("@/views/poc/svg-js-view.vue"),
        meta: { title: "SvgJs View", icon: "logos-svg" },
      },
      {
        path: "mars3d",
        name: "Mars3D",
        component: () => import("@/views/poc/mars3d.vue"),
        meta: { title: "Mars3D", icon: "earth" },
      },
    ],
  },
  {
    path: "/redirect",
    name: "redirectTo",
    component: LS_LAYOUT,
    meta: {
      title: "Redirect",
      hideBreadcrumb: true,
      hideMenu: true,
    },
    children: [
      {
        name: "redirect",
        path: "/redirect/:path(.*)",
        component: () => import("@/views/framework/redirect/index.vue"),
        meta: {
          title: "页面跳转",
          hideBreadcrumb: true,
        },
      },
    ],
  },
  {
    name: "exception",
    path: "/exception",
    component: LS_LAYOUT,
    meta: {
      title: "ErrorPage",
      hideBreadcrumb: true,
      hideMenu: true,
    },
    children: [
      {
        name: "PageNotFound",
        path: "/exception/:path(.*)*",
        component: () => import("@/views/framework/exception/index.vue"),
        meta: {
          title: "页面不存在",
          hideBreadcrumb: true,
          hideMenu: true,
        },
      },
    ],
  },
];

export const router = createRouter({
  history: createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH),
  routes,
});

/**
 * 配置路由
 * @param app App
 */
export function setupRouter(app: App) {
  app.use(router);
  setupRouterGuards(router);
}
